/*
Problem: 1200D
Date: 28-02-2024 07:37 AM
*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 2005;
int n, k;
string s;
bool arr[MAX_N][MAX_N];
int r[MAX_N], c[MAX_N];
bool r2[MAX_N][MAX_N], c2[MAX_N][MAX_N];
int prefr[MAX_N][MAX_N], prefc[MAX_N][MAX_N];
int main() {
ios::sync_with_stdio(false);
cin >> n >> k;
for(int i = 0; i < n; i++) {
cin >> s;
for(int j = 0; j < n; j++) {
arr[i][j] = (s[j] == 'B');
r[i] += arr[i][j];
c[j] += arr[i][j];
}
}
int whites = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(arr[i][j]) {
goto endloop;
}
}
whites++;
endloop:;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(arr[j][i]) {
goto endloop2;
}
}
whites++;
endloop2:;
}
for(int i = 0; i < n; i++) {
int cnt = 0;
int j;
for(j = 0; j < k; j++) {
cnt += arr[i][j];
}
for(; j < n; j++) {
r2[i][j - k] = (r[i] == 0 ? false : cnt == r[i]);
cnt += arr[i][j] - arr[i][j - k];
}
r2[i][n - k] = (r[i] == 0 ? false : cnt == r[i]);
}
for(int i = 0; i < n; i++) {
int cnt = 0;
int j;
for(j = 0; j < k; j++) {
cnt += arr[j][i];
}
for(; j < n; j++) {
c2[i][j - k] = (c[i] == 0 ? false : cnt == c[i]);
cnt += arr[j][i] - arr[j - k][i];
}
c2[i][n - k] = (c[i] == 0 ? false : cnt == c[i]);
}
// prefix sums
for(int i = 0; i < n; i++) {
int cnt = 0, cntc = 0;
int j;
for(j = 0; j < k; j++) {
cnt += r2[j][i];
cntc += c2[j][i];
}
for(; j < n; j++) {
prefr[j - k][i] = cnt;
prefc[j - k][i] = cntc;
cnt += r2[j][i] - r2[j - k][i];
cntc += c2[j][i] - c2[j - k][i];
}
prefr[n - k][i] = cnt;
prefc[n - k][i] = cntc;
}
int M = 0;
for(int i = 0; i <= n - k; i++) {
for(int j = 0; j <= n - k; j++) {
// sum of r2[l][j] for rows l = i, ..., i + k - 1
// sum of c2[l][i] for cols l = j, ..., j + k - 1
M = max(M, prefr[i][j] + prefc[j][i]);
}
}
cout << (whites + M) << endl;
}
448. Find All Numbers Disappeared in an Array | 1155. Number of Dice Rolls With Target Sum |
415. Add Strings | 22. Generate Parentheses |
13. Roman to Integer | 2. Add Two Numbers |
515. Find Largest Value in Each Tree Row | 345. Reverse Vowels of a String |
628. Maximum Product of Three Numbers | 1526A - Mean Inequality |
1526B - I Hate 1111 | 1881. Maximum Value after Insertion |
237. Delete Node in a Linked List | 27. Remove Element |
39. Combination Sum | 378. Kth Smallest Element in a Sorted Matrix |
162. Find Peak Element | 1529A - Eshag Loves Big Arrays |
19. Remove Nth Node From End of List | 925. Long Pressed Name |
1051. Height Checker | 695. Max Area of Island |
402. Remove K Digits | 97. Interleaving String |
543. Diameter of Binary Tree | 124. Binary Tree Maximum Path Sum |
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts | 501A - Contest |
160A- Twins | 752. Open the Lock |